package com.yhsimon.principle._5_ocp.improve;

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new OtherGraph());
    }
}

//这是一个用于绘图的类 [使用方]
class GraphicEditor {
    //接收 Shape 对象，然后根据 type，来绘制不同的图形
    public void drawShape(Shape s) {
       s.draw();
    }
}

//以下皆为提供方

//Shape 类，基类
abstract class Shape {
    int m_type;
    public abstract void draw();
}


class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }

    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }
}


class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

class Triangle extends Shape {
    Triangle(){
        super.m_type=3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

class OtherGraph extends Shape{
    OtherGraph(){
        super.m_type=4;
    }

    @Override
    public void draw() {
        System.out.println("绘制其他图形");
    }
}